import unittest
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
import yaml
import logging.config
import csv

def open_browser(browser, option=None):
    driver = None
    if browser == 'Chrome':
        driver = webdriver.Chrome()
    elif browser == 'Firefox':
        driver = webdriver.Firefox()
    else:
        print('浏览器选择错误！')
    return driver


class BasePage():
    # 初始化driver
    def __init__(self,driver):
        self.driver = driver

    def open_url(self, url):
        '''  打开目标网址 '''
        self.driver.maximize_window()
        self.driver.get(url)

    def find_element(self, loctor):
        '''  找单个元素 '''
        try:
            element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(loctor), f'未找到元素:{loctor}')
            return element
        except Exception as e:
            print(e)
            return False

    def find_elements(self, loctor):
        '''  找多个元素 '''
        try:
            elements = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located(loctor),
                                                            f'未找到该类元素:{loctor}')
            return elements
        except Exception as e:
            print(e)
            return False

    def if_element_text(self, locator, text):
        '''根据text的值来判断元素是否存在'''
        try:
            text = WebDriverWait(self.driver, 10).until(EC.text_to_be_present_in_element(locator, text))
        except:
            print("该元素{}不存在！".format(locator))
            return False
        else:
            return True

    def if_element_value(self, locator, text):
        '''根据value的值来判断元素是否存在'''
        try:
            texts = WebDriverWait(self.driver, 10).until(EC.text_to_be_present_in_element_value(locator, text))
        except:
            print("该元素{}不存在！".format(locator))
            return False
        else:
            return True

    def click(self, loctor):
        '''  鼠标点击 '''
        self.find_element(loctor).click()

    def send_keys(self, loctor, value):
        '''  输入值value '''
        self.find_element(loctor).send_keys(value)

    def switch_frame(self, loctor):
        ''' 转到想去的iframe '''
        self.driver.switch_to.frame(self.find_element(loctor))

    def switch_default_frame(self):
        ''' 转到默认的iframe '''
        self.driver.switch_to.default_content()

    def headles_choos(self):
        '''  切换到最新窗口 '''
        headles = self.driver.window_handles
        self.driver.switch_to.window(headles[-1])

    def delete_input_value(self, loctor):
        '''  清除内容 '''
        self.find_element(loctor).clear()

    def colose_browser(self):
        '''  关闭浏览器 '''
        self.driver.quit()

    def choose_selects_index(self, loctor, index):
        '''  下拉选择框 根据index来选择  '''
        select = Select(self.find_element(loctor))
        select.select_by_index(index)

    def choose_selects_value(self, locot, value):
        '''  下拉选择框  根据value来选择 '''
        select = Select(self.find_element(locot))
        select.select_by_value(value)

    def choose_selects_text(self, locot, text):
        '''  下拉框 根据text来选择 '''
        select = Select(self.find_element(locot))
        select.select_by_visible_text(text)


class Util:
    # 读取配置文件
    @classmethod
    def get_conf(cls):
        with open('../config/conf.yaml', 'r', encoding='UTF8')as f:
            data = yaml.load(f, Loader=yaml.FullLoader)
            return data  # 以字典格式返回配置文件中的内容

    # 截图图片
    @classmethod
    def get_screenshot(cls, driver, path):
        driver.get_screenshou_as_file(path)

    @classmethod
    def get_data_by_row(cls, file_path, line):
        logger.info('===========开始读取csv文件=============')
        with open(file_path, 'r', encoding='UTF8') as f:
            reader = csv.reader(f)
            for inder, row in enumerate(reader, 1):
                if  inder == line:
                    return row





conf_file = Util.get_conf()['LoggerConfigPath']
logging.config.fileConfig(conf_file)
logger = logging.getLogger()


if __name__ == '__main__':
    # logger.info('hello world')
    print(Util.get_data_by_row(Util.get_conf()['testData'], 2))